package com.git.xiaoyou11.service;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.hash.DecoratingStringHashMapper;
import org.springframework.data.redis.hash.HashMapper;
import org.springframework.data.redis.hash.JacksonHashMapper;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.data.redis.support.collections.DefaultRedisList;
import org.springframework.data.redis.support.collections.DefaultRedisMap;
import org.springframework.data.redis.support.collections.RedisList;
import org.springframework.data.redis.support.collections.RedisMap;

import com.git.xiaoyou11.support.KeyGenUtils;


/****
	 * spring-redis-repository
	 * @author xiaoyou11@hotmail.com
	 *
 */
public class BaseClientRedisRepository<T> {
	
	@Autowired
	private StringRedisTemplate template;
	public StringRedisTemplate getTemplate() {
		return template;
	}

	//计数器
	private RedisAtomicLong entityIdCounter;
	
	private Class<T> entityClass;
	// 时间轴
	private RedisList<String> timeline;
	private HashMapper<T, String, String> postMapper;
		
	public HashMapper<T, String, String> getPostMapper() {
		return postMapper;
	}

	@SuppressWarnings("unchecked")
	@PostConstruct
	public void postConstruct() {
		//定义Class类型
		Type type = getClass().getGenericSuperclass();
		if (type instanceof ParameterizedType) {
			entityClass = (Class<T>) ((ParameterizedType) type).getActualTypeArguments()[0];
		}
		postMapper = new DecoratingStringHashMapper<T>(new JacksonHashMapper<T>((Class<T>) entityClass));
		entityIdCounter = new RedisAtomicLong(KeyGenUtils.globalEntityId(entityClass.getSimpleName()), template.getConnectionFactory());
		timeline = new DefaultRedisList<String>(KeyGenUtils.timeline(entityClass.getSimpleName()), template); 
	}
	
	private RedisMap<String, String> getEntityMap(String itemId) {
		return new DefaultRedisMap<String, String>(KeyGenUtils.buildMapRecordKey(entityClass.getSimpleName(),itemId), template);
	}
	
	/**
	 * 添加一条对象
	 * @param n
	 */
	public void addEntity(T item){
		String itemId = String.valueOf(entityIdCounter.incrementAndGet());
		getEntityMap(itemId).putAll(postMapper.toHash(item));
		timeline.addFirst(itemId);
	}
	
	/**
	 * 添加多个对象
	 * @param name
	 */
	public void addEntityList(List<T> items) {
		for(T item : items){
			String itemId = String.valueOf(entityIdCounter.incrementAndGet());
			getEntityMap(itemId).putAll(postMapper.toHash(item));
		}
	}
	
	/**
	 * 根据id查询对象
	 * @param id
	 * @return
	 *//*
	public New getNew(String id){
		System.out.println(KeyGenUtils.news(id));
		New n = postMapper.fromHash(getNewsMap(KeyGenUtils.news(id)));
		return n;
	}
	*/
	
	/**
	 * 删除对象
	 * @param n
	 * @return
	 */
	public boolean removeEntity(T item){
		return true;
	}
}
